package com.sifang.POI.dianping;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



public class DpCmd {
	
	private static final int READ_BATCH = 10000;
	private static DpCmd instance;
	private BufferedReader reader;
	
	public synchronized static DpCmd getInstance() {
		if (instance == null) {
			instance = new DpCmd();
		}
		return instance;
	}
	
	protected DpCmd() {
		this.reader = new BufferedReader(new InputStreamReader(System.in));
	}
	
	private String readLine() throws IOException {
		return this.reader.readLine();
	}
	
	public int getDbReadBatchSize() {
		return READ_BATCH;
	}
	
	public void start() throws Exception {
		System.out.println("dianping command line started");
		for (;;) {
			System.out.print("> ");
			String cmd = this.readLine();
			String[] args = cmd.split("\\s");
			if (args[0].equalsIgnoreCase("exit")) {
				break;
			} else {
				try {
					dispatch(args);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @param args
	 * @throws Exception 
	 * @throws IOException 
	 */
	public static void main(String[] args) {
		try {
			DpCmd.getInstance().start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void dispatch(String[] args) throws Exception {
		String action = args[0];
		if (action.equalsIgnoreCase("addcity")) {
			if (args.length < 2) {
				System.out.println("city id required");
			} else {
				int cityId = Integer.parseInt(args[1]);
				new AddCityCommand(cityId).execute();
			}
		} else if (action.equalsIgnoreCase("prejob")) {
			if (args.length < 2) {
				new PreJobCommand().execute();
			} else {
				int maxThreads = Integer.parseInt(args[1]);
				new PreJobCommand(maxThreads).execute();
			}
		} else if (action.equalsIgnoreCase("job")) {
			if (args.length < 2) {
				new JobCommand().execute();
			} else {
				int maxThreads = Integer.parseInt(args[1]);
				new JobCommand(maxThreads).execute();
			}
		} else if (action.equalsIgnoreCase("adddetail")) {
			if (args.length < 2) {
				new AddDetailCommand().execute();
			} else {
				int maxThreads = Integer.parseInt(args[1]);
				new AddDetailCommand(maxThreads).execute();
			}
		} else if (action.equalsIgnoreCase("adddistrict")) {
			if (args.length < 2) {
				System.out.println("city id required");
			} else {
				int cityId = Integer.parseInt(args[1]);
				new AddDistrictCommand(cityId).execute();
			}
		} else if (action.equalsIgnoreCase("addcategory")) {
			if (args.length < 2) {
				System.out.println("city id required");
			} else {
				int cityId = Integer.parseInt(args[1]);
				new AddCategoryRegionCommand(cityId).execute();
			}
		} else if (action.equalsIgnoreCase("go")) {
			if (args.length < 2) {
				System.out.println("city id required");
			} else if (args.length == 2) {
				int cityId = Integer.parseInt(args[1]);
				new GoCommand(cityId).execute();
			} else {
				int cityId = Integer.parseInt(args[1]);
				int maxThreads = Integer.parseInt(args[2]);
				new GoCommand(cityId, maxThreads).execute();
			}
		}
	}

}
